<template>
  <div class="upload-img">
    <div class="upload-list" v-for="(item, index) in uploadList" :key="index">
      <template v-if="item.status === 'finished'">
        <img :src="item.url">
        <div class="upload-list-cover">
          <Icon type="ios-eye-outline" @click.native="handleView(item.name)"></Icon>
          <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
        </div>
      </template>
      <template v-else>
        <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
      </template>
    </div>
    <Upload
      ref="upload"
      :show-upload-list="false"
      :default-file-list="defaultImageList"
      :on-success="handleSuccess"
      :format="['jpg','jpeg','png']"
      :max-size="10*1024"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :before-upload="handleBeforeUpload"
      multiple
      type="drag"
      :action="uploadUrl + '/imageController/upload/'"
      style="display: inline-block; width: 100px;">
      <div style="width: 100px; height:100px; line-height: 100px;">
        <Icon type="camera" size="20"></Icon>
      </div>
    </Upload>
    <Modal title="查看图片" v-model="visible">
      <img :src="cdnUrl + '/' + imgName" v-if="visible" style="width: 100%">
    </Modal>
  </div>
</template>

<script>
  import { uploadUrl, cdnUrl } from '@/config'

  export default {
    name: 'upload-img',
    props: [
      'defaultImageList',
      'maxLength'
    ],
    data () {
      return {
        uploadUrl,
        cdnUrl,
        imgName: '',
        visible: false,
        uploadList: []
      }
    },
    methods: {
      handleView (name) {
        this.imgName = name
        this.visible = true
      },
      handleRemove (file) {
        // 从 upload 实例删除数据
        const fileList = this.$refs.upload.fileList
        this.$refs.upload.fileList.splice(fileList.indexOf(file), 1)
        this.$emit('on-change', this.$refs.upload.fileList)
      },
      handleSuccess (res, file) {
        // 因为上传过程为实例，这里模拟添加 url
        file.url = `${cdnUrl}/${res.responseData}`
        file.name = `${res.responseData}`
        this.$emit('on-change', this.$refs.upload.fileList)
      },
      handleFormatError (file) {
        this.$Notice.warning({
          title: '文件格式不正确',
          desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
        })
      },
      handleMaxSize (file) {
        this.$Notice.warning({
          title: '超出文件大小限制',
          desc: '文件 ' + file.name + ' 太大，不能超过 10M。'
        })
      },
      handleBeforeUpload () {
        const check = this.uploadList.length < (this.maxLength || 5)
        if (!check) {
          this.$Notice.warning({
            title: `最多只能上传 ${this.maxLength || 5} 张图片，请删除后再上传`
          })
        }
        return check
      }
    },
    mounted () {
      this.uploadList = this.$refs.upload.fileList
    }
  }
</script>

<style lang="less">
  .upload-img {
    .upload-list {
      display: inline-block;
      width: 100px;
      height: 100px;
      text-align: center;
      line-height: 100px;
      border: 1px solid transparent;
      border-radius: 4px;
      overflow: hidden;
      background: #fff;
      position: relative;
      box-shadow: 0 1px 1px rgba(0,0,0,.2);
      margin-right: 4px;
      img {
        width: 100%;
        height: 100%;
      }
    }

    .upload-list-cover {
      display: none;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      background: rgba(0,0,0,.6);
      i {
        color: #fff;
        font-size: 26px;
        cursor: pointer;
        margin: 0 8px;
      }
    }

    .upload-list:hover .upload-list-cover {
        display: block;
    }

     .ivu-upload {
      width: 100px;
      height: 100px;
      .ivu-icon {
        margin-top: 20px;
      }
    }
  }
</style>
